<?php
/**
 * @Author: Ron Chen
 * @Date:   2017-03-25 00:56:58
 * @Last Modified by:   Ron Chen
 * @Last Modified time: 2017-03-25 22:09:35
 */
namespace app\models;

use Yii;

/**
 * [************已经弃用************]
 *
 * @property integer $id
 * @property string $project_code
 * @property string $plan_code
 * @property string $plan_name
 * @property string $benifit_code
 * @property string $benifit_name
 * @property string $benifit_amount
 * @property string $benifit_currency
 */
class InsuranceUser extends \yii\db\ActiveRecord
{
	const USER_SEX_NO = 0;
	const USER_SEX_MALE = 1;
	const USER_SEX_FEMALE = 2;

	public static $user_info_sex = [
		self::USER_SEX_MALE,self::USER_SEX_FEMALE
	];

	const USER_STATUS_SUCCESS = 1;
	const USER_STATUS_FAILD   = 0;

	

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'insurance_user';
    }
    
    /**
     * 初始化用户操作
     */
    public static function initRecord($param){
    	$project_id = isset($param["project_id"]) ? $param["project_id"] : 0;
    	$account_id = isset($param["account_id"]) ? $param["account_id"] : 0;
    	
    	$userInfo = static::find()->where(['project_id'=>$project_id,'account_id'=>$account_id])->limit(1)->one();
    	if (empty($userInfo)) {
    		$userInfo = new static();
    		$userInfo->ctime  = time();
            $userInfo->project_id = $project_id;
            $userInfo->account_id = $account_id;
    	}
    	if (isset($param['name']) && $param['name']) {
    		$userInfo->name = $param['name'];
    	}
    	if (isset($param['id_type']) && $param['id_type']) {
    		$userInfo->id_type = $param['id_type'];
    	}
    	if (isset($param['id_card']) && $param['id_card']) {
    		$userInfo->id_card = $param['id_card'];
    	}
    	if (isset($param['birthday']) && $param['birthday']) {
    		$userInfo->birthday = $param['birthday'];
    	}
    	if (isset($param['sex']) && $param['sex'] && in_array($param['sex'], self::$user_info_sex)) {
    		$userInfo->sex = $param['sex'];
    	}
    	if (isset($param['phone']) && $param['phone']) {
    		$userInfo->phone = $param['phone'];
    	}
    	if (isset($param['email']) && $param['email']) {
    		$userInfo->email = $param['email'];
    	}

    	$userInfo->status = self::USER_STATUS_SUCCESS;
    	$userInfo->utime  = time();

    	if ($userInfo->save()) {
    		return $userInfo->attributes['id'];
    	}
    	return false;
    }
}
